<?php

$conn = @mysqli_connect('localhost','wd1900144','pass123456');
if($conn == false){
  echo json_encode(['error'=>1,'msg'=>'网络开小差了，嘤嘤嘤']);
}

mysqli_select_db($conn,'wd1900144');

mysqli_query($conn, 'set names utf8');



if($_POST['act']=='reg'){
    $ruser =  $_POST['ruser'];
    $remail =  $_POST['remail'];
    $rpwd =  $_POST['rpwd'];
    $rsex =  $_POST['rsex'];
    $rphone =  $_POST['rphone'];
    $rdesc =  $_POST['rdesc'];


    $query = mysqli_query($conn,"SELECT * FROM so_user WHERE email='$remail' || phone=$rphone" );
    // print_r(mysqli_num_rows($query));
    if(mysqli_num_rows($query)>0){//分号一定要记得加
        $info = mysqli_fetch_assoc($query);//某个用户的数据，一位数组

        $n =  $info['email']==$remail?'邮箱':'手机号';//判断是邮箱重复还是手机号重复

        echo json_encode(['error'=>1,'msg'=>$n.'已被注册']);
    exit();
    }
    //添加用户
    mysqli_query($conn,"INSERT INTO so_user(phone,pwd,nickname,email,sex,descs) VALUES('$rphone','$rpwd','$ruser','$remail','$rsex','$rdesc') ");
    echo json_encode(['error'=>0,'msg'=>'注册成功']);
}
else if($_POST['act']=='login'){
  $lphone = $_POST['lphone'];
  $lpwd = $_POST['lpwd'];
  $query = mysqli_query($conn,"SELECT * FROM so_user WHERE phone=$lphone && pwd = '$lpwd' " );

  if(mysqli_num_rows($query)>0){
    $num = mysqli_num_rows($query);
    $info = mysqli_fetch_assoc($query);//读取用户信息
    echo json_encode(['error'=>0,'msg'=>'登录成功','data'=>$info,'num'=>$num]);
    // var_dump($info);  
  }else{
    //没有记录
    echo json_encode(['error'=>1,'msg'=>'用户不存在']);
  }
}
else if($_POST['act']=='wedata'){
    $nickname = $_POST['nickname'];	
    $email = $_POST['email'];
    $sex = $_POST['sex'];	
    $descs = $_POST['descs'];
    $phone = $_POST['phone'];
    $uid = $_POST['uid'];
    // 保存图片
  // var_dump($_POST['phone']);
      $parr = array();
      for($i=1; $i<=3; $i++){
        if(isset($_POST['photo'.$i])){		// isset(变量$_POST['pic2']) 函数，判断变量是否存在
          // 上传图片
        }else{
        break;
        }
      }
      $ii = $i-1;
      // var_dump($ii);
      $photo = $_POST['photo'.$ii];
      // var_dump($_POST['photo1']);
      if(strstr($photo,',')){
        $arr = explode(',', $photo);  // 字符串转数组
        $photo = $arr[1];		//判断是否有逗号 如果有就截取后半部分
      }
      $name = date('YmdHis').mt_rand(10000,99999).'.png';   // mt_rand()  随机数
      // 创建文件（如：1.png）,在往文件里面写入内容     file_put_contents('文件地址+名','内容')
      file_put_contents('../uploads/'.$name, base64_decode($photo)); 
      $parr[] = $name;  // 把3张图片名称，追加到数组里面
      $photos = implode(',', $parr);	//把数组分割成字符串，逗号隔开$photos这是数据整理完的值
      
     
      
      $query2 = mysqli_query($conn,"UPDATE `so_user` SET `photo` = '$photos', `nickname` = '$nickname', `email` = '$email' , `sex` = '$sex', `phone` = '$phone', `descs` = '$descs'  WHERE `so_user`.`uid` = $uid ");
      $query1 = mysqli_query($conn,"SELECT * FROM so_user WHERE uid=$uid");
      
      $info = mysqli_fetch_assoc($query1);//读取用户信息
    // var_dump($info);
      echo json_encode(['error'=>0,'msg'=>'修改成功','data'=>$info]);


}

else if($_POST['act']=='focus'){
  $uid= $_POST['uid'];
  $touid= $_POST['touid'];


 $query = mysqli_query($conn,"select*from so_focus where uid=$uid and touid=$touid");
 if(mysqli_num_rows($query)>0){
  mysqli_query($conn,"delete from so_focus where uid=$uid and touid=$touid");

 }else{
  mysqli_query($conn,"insert into so_focus(uid,touid) values($uid,$touid)");
 }
// var_dump(mysqli_num_rows($query));
 echo json_encode(['error'=>0,'msg'=>'success']);

}



mysqli_close($conn);

?>